﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Zwms.Client.CommonUI;
using Zwms.Data.BusinessObjects;
using Zwms.Data.CommonObject;
using grproLib;
using Zwms.Client.Helper;
using Zwms.Client.InfoSys;

namespace Zwms.Client.InWms
{
    public partial class PrintBarCodeForm : BaseForm
    {
        #region  成员变量
        GridppReport Report = new GridppReport();
        private IGRField _barCodeField;
        private IGRField _goodsNameField;
        private IList<BilInPlanBillDetail> _inPlanBillDetailList;
        #endregion

        #region 构造函数
        public PrintBarCodeForm(string sInPlanBillCode,DateTime dt)
        {
            InitializeComponent();

            tbInPlanBillCode.Text = sInPlanBillCode;
            ServiceReferenceZwms.ServiceZwmsClient client
                = new ServiceReferenceZwms.ServiceZwmsClient();
           
            BilInPlanBill inPlanBillRecord = new BilInPlanBill();
            inPlanBillRecord.Id = sInPlanBillCode;
            inPlanBillRecord = client.QueryInPlanBill(inPlanBillRecord);
            if (inPlanBillRecord.BillStatus == "0")  //0 为等待打印标签状态
            {
                inPlanBillRecord.BillStatus = "1";   //1 为正在收获阶段
            }
            if (client.UpdateInPlanBill(inPlanBillRecord))
            {
                SysLogWriter.WriteOptLog("打印条码更新入库计划单", inPlanBillRecord.Id, "改");
                BilInPlanBillDetail record = new BilInPlanBillDetail();
                record.IBPlanCode = sInPlanBillCode;
                PagerIndex pagerindex = new PagerIndex(1, int.MaxValue);
                _inPlanBillDetailList = client.QueryInPlanBillDetailList(record, ref pagerindex);
            }
            else
            {
                MessageBox.Show("获取入库计划单失败！");
                this.Close();
            }
            tbInDate.Text = CommonHelper.GetDateString(dt);
            InitBtnImage();
            InitControl();
        }
        #endregion

        #region 私有函数
        private void InitBtnImage()
        {
            this.btnPrintSingle.Image = global::Zwms.Client.Properties.Resources.barcode;
            this.btnPrint.Image = global::Zwms.Client.Properties.Resources.print;
            this.btnCancel.Image = global::Zwms.Client.Properties.Resources.cancel;

        }
        private void InitControl()
        {
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();

            IList<BilInPlanBillDetail> tempList = new List<BilInPlanBillDetail>();
            for (int i = 0; i < _inPlanBillDetailList.Count; i++)
            {
                BilInPlanBillDetail record = _inPlanBillDetailList[i];
                record.GoodsBarCode = string.Format("{0}#{1}", record.GoodsCode, tbInDate.Text);
                client.UpdateInPlanBillDetail(record);
            }
            this.bilInPlanBillDetailBindingSource.DataSource = null;
            this.bilInPlanBillDetailBindingSource.DataSource = _inPlanBillDetailList;
        }

        private void SelectConfirm()
        {
            if (dgv.CurrentCell == null)
            {
                MessageBox.Show("请选择要打印的标签项");
                return;
            }
            int rowIndex = dgv.CurrentCell.RowIndex;

            if (rowIndex < 0)
            {
                return;
            }
            DataGridViewRow row = dgv.Rows[rowIndex];
            BilInPlanBillDetail record = row.DataBoundItem as BilInPlanBillDetail;
            IList<BilInPlanBillDetail> recordList = new List<BilInPlanBillDetail>();
            recordList.Add(record);

            PrintBarCodeAssistForm window = new PrintBarCodeAssistForm(recordList, tbInDate.Text);
            window.ShowDialog();
        }

        #endregion

        #region 控件函数
        private void btnPrintSingle_Click(object sender, EventArgs e)
        {
            SelectConfirm();
        }

        private void dgv_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            SelectConfirm();
        }

        private void btnPrintPreview_Click(object sender, EventArgs e)
        {
            if (_inPlanBillDetailList == null || _inPlanBillDetailList.Count == 0)
            {
                MessageBox.Show("需要打印标签的货物数量为0！");
                return;
            }
            PrintBarCodeAssistForm window = new PrintBarCodeAssistForm(_inPlanBillDetailList, tbInDate.Text);
            window.ShowDialog();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
        #endregion
    }
}
